package com.cardpay.bill;

import com.cardpay.common.db.IRowMapper;
import com.cardpay.common.db.JdbcTemplate;
import com.cardpay.common.util.SQLHelper;
import com.cardpay.page.PageBean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by asusa on 2017/1/24.
 */
public class BillDao {

    private JdbcTemplate template = new JdbcTemplate();

    public List<Bill> list(){
        final String sql = "SELECT * FROM t_bill";
        return this.template.queryForList(sql, new BillRowMapper());
    }

    /**
     * 新增账单
     *
     * @param bill
     *            封装了待插入账单信息的对象
     * @return 受影响的行数
     */
    public int add(Bill bill) {
        final String sql = "INSERT INTO t_bill(id, name, num, unit, money, isPayment, supplierId, descriptions, createTime) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";

        Object[] params = new Object[] { //
                bill.getId(), //
                bill.getName(),//
                bill.getNum(),//
                bill.getUnit(),//
                bill.getMoney(),//
                bill.getIsPayment(),//
                bill.getSupplierId(),//
                bill.getDescriptions(),//
                bill.getCreateTime()//
        };

        return this.template.update(sql, params);
    }

    public int delete(String id) {
        final String sql = "DELETE FROM t_bill WHERE id = ?";

        return this.template.update(sql, id);
    }

    public int update(Bill bill) {
        final String sql = "UPDATE t_bill SET name=?, num=?, unit=?, money=?, isPayment=?, supplierId=?, descriptions=? WHERE id=?";

        Object[] params = new Object[] { //
                bill.getName(),//
                bill.getNum(),//
                bill.getUnit(),//
                bill.getMoney(),//
                bill.getIsPayment(),//
                bill.getSupplierId(),//
                bill.getDescriptions(),//
                bill.getId()
        };

        return this.template.update(sql, params);
    }

    /**
     * 根据账单id查询账单
     *
     * @param id
     *            账单
     * @return 账单对象
     */
    public Bill find(String id) {
        final String sql = "SELECT * FROM t_bill WHERE id=?";

        return this.template.query(sql, new BillRowMapper(), id);
    }

    /**
     * 分页查询账单列表（账单列表及分页信息封装于PageBean中）
     *
     * @param pageNum
     *            当前页码
     * @param pageSize
     *            每页显示记录数
     * @param sqlHelper
     *            封装了分页查询语句以及查询条件的SQLHelper对象
     * @return 分页信息对象
     */
    public PageBean<Bill> findPagingBill(int pageNum, int pageSize, SQLHelper sqlHelper) {
        return this.template.pagination(pageNum, pageSize, sqlHelper, new BillRowMapper());
    }

    public class BillRowMapper implements IRowMapper<Bill> {
        @Override
        public Bill mapRow(ResultSet rs) throws SQLException {
            Bill bill=new Bill();
            bill.setId(rs.getString("id"));
            bill.setName(rs.getString("name"));
            bill.setNum(rs.getInt("num"));
            bill.setUnit(rs.getString("unit"));
            bill.setMoney(rs.getDouble("money"));
            bill.setIsPayment(rs.getString("isPayment"));
            bill.setSupplierId(rs.getString("supplierId"));
            bill.setDescriptions(rs.getString("descriptions"));
            bill.setCreateTime(rs.getString("createTime"));
            return bill;
        }
    }
}
